﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using UnityEngine.UI;

public class RedDot:MonoBehaviour
{
    public Image imgMail;
    public Image imgMailSystem;
    public Image imgMailTeam;

    public Text txtMail;
    public Text txtMailSystem;
    public Text txtMailTeam;

    private void Start()
    {
        RedPointSystem rps = new RedPointSystem();
        rps.InitRedPointTreeNode();

        rps.SetRedPointNodeCallBack(RedPointConst.mail, MailCallBack);
        rps.SetRedPointNodeCallBack(RedPointConst.mailSystem, MailSystemCallBack);
        rps.SetRedPointNodeCallBack(RedPointConst.mailTeam, MailTeamCallBack);

        rps.SetInvoke(RedPointConst.mailSystem, 3);
        rps.SetInvoke(RedPointConst.mailTeam, 4);


    }
    void MailCallBack(RedPointNode node)
    {
        txtMail.text = node.pointNum.ToString();
        imgMail.gameObject.SetActive(node.pointNum > 0);
        
    } 
    void MailSystemCallBack(RedPointNode node)
    {
        txtMailSystem.text = node.pointNum.ToString();
        imgMailSystem.gameObject.SetActive(node.pointNum > 0);
    }
    void MailTeamCallBack(RedPointNode node)
    {
        txtMailTeam.text = node.pointNum.ToString() ;
        imgMailTeam.gameObject.SetActive(node.pointNum > 0);
    }
}
/// <summary>
/// 红点数结构常量String
/// </summary>
public class RedPointConst
{
    public const string main = "Main";//主界面
    public const string mail = "Main.Mail";//主界面邮件
    public const string mailSystem = "Main.Mail.System";//邮件系统界面
    public const string mailTeam = "Main.Mail.Team";//邮件队伍界面
    public const string mailAlliance = "Main.Mail.Alliance";//邮件工会界面
    public const string task = "Main.Task";//主界面任务
    public const string alliance = "Main.Alliance";//主界面工会

}
/// <summary>
/// 节点单个单位
/// </summary>
public class RedPointNode
{
    public string nodeName;//节点名称
    public int pointNum = 0;//红点数量
    public RedPointNode parent = null;// 父节点
    public RedPointSystem.OnPointNumChange numChangeFunc;//发生变化后回调函数
    public Dictionary<string,RedPointNode>dicChilds = 
        new Dictionary<string, RedPointNode>();//子节点

    /// <summary>
    /// 设置当前节点的红点数量
    /// </summary>
    /// <param name="rpNum"></param>
    public void SetRedPointNum(int rpNum)
    {
        if (dicChilds.Count>0)//红点数量只能设置叶子节点
        {
            return;
        }
        pointNum = rpNum;
        NotifyPointNumChange();
        if (parent!= null)
        {
            parent.ChangePredPointNum();
        }
    }
    /// <summary>
    /// 计算当前红点数量
    /// </summary>
    public void ChangePredPointNum()
    {
        int num = 0;
        foreach(var node in dicChilds.Values)
        {
            num += node.pointNum;
        }
        if (num!= pointNum)//红点有变化
        {
            pointNum = num;
            NotifyPointNumChange();
        }
        if(parent != null)
        {
            parent.ChangePredPointNum();
        }
    }
    /// <summary>
    /// 通知红点数量变化
    /// </summary>
    public void NotifyPointNumChange()
    {
        numChangeFunc?.Invoke(this);
    }
}
public class RedPointSystem
{
    public delegate void OnPointNumChange(RedPointNode node);//红点变化通知
    RedPointNode mRootNode;//红点树Root节点
    static List<string> IstRedPointTreeList = new List<string>//初始化红点数
    {
        RedPointConst.main,
        RedPointConst.mail,
        RedPointConst.mailSystem,
        RedPointConst.mailTeam,
        RedPointConst.mailAlliance,
        RedPointConst.task,
        RedPointConst.alliance,
    };
    /// <summary>
    /// 初始化树节点
    /// </summary>
    public void InitRedPointTreeNode()
    {
        mRootNode = new RedPointNode();//建立新节点 为主节点
        mRootNode.nodeName = RedPointConst.main;//设置主节点名字
        foreach (var s in IstRedPointTreeList)//循环初始化的所有节点
        {
            var node = mRootNode;
            var treeNodeAy = s.Split('.');
            if (treeNodeAy[0]!=mRootNode.nodeName)//判断不是此红点数下的子节点
            {
                Debug.Log("Node Error::"+treeNodeAy[0]);//报错，跳过此次循环
                continue;
            }
            if (treeNodeAy.Length > 1)//大于1 有子节点
            {
                for (int i = 1 ; i < treeNodeAy.Length; i++)
                {
                    if (!node.dicChilds.ContainsKey(treeNodeAy[i]))//判断字典有没有treeNodeAy[i]节点
                    {
                        node.dicChilds.Add(treeNodeAy[i], new RedPointNode());//加一个新节点存放子节点数据
                        node.dicChilds[treeNodeAy[i]].nodeName = treeNodeAy[i];
                        node.dicChilds[treeNodeAy[i]].parent = node;
                    }
                    node = node.dicChilds[treeNodeAy[i]];
                }
            }
        }
    }
    public void SetRedPointNodeCallBack(string strNode, RedPointSystem.OnPointNumChange callBack)
    {
        var nodeList = strNode.Split('.');
        if (nodeList.Length == 1)
        {
            if (nodeList[0] != RedPointConst.main)
            {
                Debug.Log("Get Wrong Root Node,current id:" + nodeList[0]);
                return;
            }
        }
        var node = mRootNode;//获取主节点
        for (int i = 1; i < nodeList.Length; i++)
        {
            if (!node.dicChilds.ContainsKey(nodeList[i]))
            {
                Debug.Log("Does Not Contains Child Node:" + nodeList[i]);
                return;
            }
            node = node.dicChilds[nodeList[i]];
            if (i == nodeList.Length-1)
            {
                node.numChangeFunc = callBack;//绑定回调
                return;
            }
        }
    }

    /// <summary>
    /// 找到对应的节点，给它设置具体的红点数量
    /// </summary>
    /// <param name="strNode"></param>
    /// <param name="rpNum"></param>
    public void SetInvoke(string strNode,int rpNum)
    {
        var nodeList = strNode.Split('.');
        if (nodeList.Length == 1)
        {
            if (nodeList[0]!= RedPointConst.main)
            {
                Debug.Log("Root节点错误:" + nodeList[0]);
                return;
            }
        }
        var node = mRootNode;
        for (int i = 1; i < nodeList.Length; i++)
        {
            if (!node.dicChilds.ContainsKey(nodeList[i]))
            {
                Debug.Log("未找到当前节点:" + nodeList[i]);
                return;
            }
            node = node.dicChilds[nodeList[i]];
            if (i == nodeList.Length-1)
            {
                node.SetRedPointNum(rpNum);
            }
        }
    }
   

}
